﻿//License.cs
//Copyright (c) 2013 StockSharp LLC, all rights reserved.
//This code module is part of StockSharp library.
//This code is licensed under the GNU GENERAL PUBLIC LICENSE Version 3.
//See the file License.txt for the license details.
//More info on: http://stocksharp.com

namespace StockSharp.Algo.Licensing
{
	using System;
	using System.Text;
	using System.Xml.Linq;

	using Ecng.Common;

	/// <summary>
	/// Лицензия.
	/// </summary>
	public class License
	{
		internal License(byte[] body)
		{
			if (body == null)
				throw new ArgumentNullException("body");

			Body = body;

			var xml = Encoding.UTF8.GetString(body).To<XElement>();

			Id = xml.GetElementValue<long>("id");
			IssuedTo = xml.GetElementValue("company");
			IssuedDate = xml.GetElementValue<long>("issuedDate").To<DateTime>();
			ExpirationDate = xml.GetElementValue<long>("expirationDate").To<DateTime>();
			HardwareId = xml.GetElementValue("hardwareId");
			Features = xml.GetElementValue("features").Split(',');
			Signature = xml.GetElementValue("signature").Base64();

			xml.Element("signature").Remove();
			BodyWithoutSignature = Encoding.UTF8.GetBytes(xml.To<string>());
		}

		/// <summary>
		/// Номер лицензии.
		/// </summary>
		public long Id { get; private set; }

		/// <summary>
		/// Получатель лицензии (ФИО частного лица или название компании).
		/// </summary>
		public string IssuedTo { get; private set; }

		/// <summary>
		/// Дата выдачи лицензии.
		/// </summary>
		public DateTime IssuedDate { get; private set; }

		/// <summary>
		/// Дата окончания действия лицензии.
		/// </summary>
		public DateTime ExpirationDate { get; private set; }

		/// <summary>
		/// Идентификатор компьютера.
		/// </summary>
		public string HardwareId { get; private set; }

		/// <summary>
		/// Список доступной функциональности.
		/// </summary>
		public string[] Features { get; private set; }

		/// <summary>
		/// Цифровая подпись.
		/// </summary>
		public byte[] Signature { get; private set; }

		/// <summary>
		/// Тело лицензии.
		/// </summary>
		public byte[] Body { get; private set; }

		internal byte[] BodyWithoutSignature { get; private set; }
	}
}